package ch03;

import java.util.Arrays;
import java.util.Iterator;

//基于拉链法的散列表
public class SeparateChainingHashST<Key, Value> {
    private SequentialSearchST[] st;
    private int M;
    private int N;

    public SeparateChainingHashST() {
        this(997);
    }

    public SeparateChainingHashST(int M) {
        this.M = M;
        st = new SequentialSearchST[M];
        for (int i = 0; i < M; i++) {
            st[i] = new SequentialSearchST();
        }
    }

    private int hash(Key key) {
        return (key.hashCode() & 0xfffffff) % M;
    }

    public Value get(Key key) {
        return (Value) st[hash(key)].get(key);
    }

    public void put(Key key, Value value) {
        int put = st[hash(key)].put(key, value);
        if (put == 1) {
            N++;
        }
    }




    public Iterable<String> keys(){
        return () ->{
            return new Iterator<String>(){
                private int index = 0;
                @Override
                public boolean hasNext() {
                    return index<M;
                }

                @Override
                public String next() {
                    return st[index++].values();
                }
            };
        };
    }



    public static void main(String[] args) {
        SeparateChainingHashST<String, Integer> sst = new SeparateChainingHashST<String, Integer>();
        sst.put("sdx", 43);
        sst.put("dsc", 12);
        sst.put("cxvsd", 1);
        sst.put("asdq", 65);
        sst.put("saxc", 33);
        Iterator<String> iterator = sst.keys().iterator();
        while (iterator.hasNext()) {
            System.out.print(iterator.next());

        }

    }


}
